import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:shop/shop/merchant/orders/OrderIng.dart';
import 'package:shop/shop/merchant/orders/OrderWancheng.dart';
import 'package:shop/config/config.dart';
import 'package:shop/http/HttpUtil.dart';
import 'package:shop/route/my_route.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
class MyOrderList extends StatefulWidget {
  MyOrderList({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyOrderList createState() => _MyOrderList();
}

class _MyOrderList extends State<MyOrderList> with SingleTickerProviderStateMixin {
  TabController mController;// tab控制器
   List<Tab> titleTabs = <Tab>[
    Tab(
      text: '进行中(3)',
    ),
    Tab(
      text: '已完成(27)',
    ),
  ];

  @override
  void initState() {
    super.initState();
    _getMerchantOrderNum();
    mController = TabController(initialIndex: 0,length:titleTabs.length, vsync: this);
    // 初始化TabController
    // 参数1：初试显示的tab位置
    // 参数2：tab的个数
    // 参数3：动画效果的异步处理，默认格式

    // 添加监听器
    mController.addListener(() => _onTabChanged());
  }

  // 点击监听函数
  _onTabChanged() {
    print(mController.index);
  }

  String merchantId;//商家ID
  SharedPreferences prefs;
  _getMerchantOrderNum() async {
    prefs = await SharedPreferences.getInstance();
    merchantId = prefs.getString("merchantID");
    var x = await HttpUtil().get("/waimai/mOder/getMerchantOrderNum?merchantId="+merchantId);
    if(x['code']==200){
      print("进行中："+x['data']['jinxingzhong'].toString());
      print("已完成："+x['data']['yiwancheng'].toString());
      titleTabs.removeWhere((element) => true);
      titleTabs.add(Tab(text: '进行中('+x['data']['jinxingzhong'].toString()+")",));
      titleTabs.add(Tab(text: '已完成('+x['data']['yiwancheng'].toString()+")",));
      setState(() {

      });
    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context , width: 464 , height: 820) ;
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: TabBar(
          controller: mController,// 设置控制器
          labelColor: Colors.white, //选中的颜色
          labelStyle: TextStyle(fontSize: 16), //选中的样式
          unselectedLabelColor: Colors.black, //未选中的颜色
          unselectedLabelStyle: TextStyle(fontSize: 14), //未选中的样式
          indicatorColor: Colors.white, //下划线颜色
          isScrollable: true, //是否可滑动，设置不可滑动，则是tab的宽度等长
          //tab标签
          tabs: titleTabs, // 设置标题
          //点击事件
          onTap: (int i) {
            print(i+10);
          },
        ) ,
        elevation: 0,
        // bottom:TabBar(
        //   controller: mController,// 设置控制器
        //   labelColor: Colors.white, //选中的颜色
        //   labelStyle: TextStyle(fontSize: 16), //选中的样式
        //   unselectedLabelColor: Colors.black, //未选中的颜色
        //   unselectedLabelStyle: TextStyle(fontSize: 14), //未选中的样式
        //   indicatorColor: Colors.white, //下划线颜色
        //   isScrollable: true, //是否可滑动，设置不可滑动，则是tab的宽度等长
        //   //tab标签
        //   tabs: titleTabs, // 设置标题
        //   //点击事件
        //   onTap: (int i) {
        //     print(i+10);
        //   },
        // ) ,
      ),
     body: Container(
    padding: EdgeInsets.only(top:20),
    child: new TabBarView(
    controller: mController,
    children:  <Widget>[ // 每个空间对应的页面
      OrderIng(),
      OrderWancheng()
    ],
    )
    ,),
    );
  }
}